package com.crm.system.service.impl.sys;

import com.crm.common.util.CommonUtils;
import com.crm.model.entity.sys.ImportExport;
import com.crm.model.entity.sys.UserAccount;
import com.crm.service.sys.ImportExportDbService;
import com.crm.system.service.sys.ImportExportService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author luojie
 * @date 2021/6/8 15:29
 */
@Service
public class ImportExportServiceImpl implements ImportExportService {

    @Autowired
    private ImportExportDbService importExportDbService;

    /***
     * @Description: 插入导入表
     * @Param: operateQuantity 数量
     * @Param: operateResult 结果：0-false；1-true；
     * @Param: operateWay 导入还是导出：1-导入；2-导出
     * @Param: operateType 导入/导出什么业务：1-线索；2-客户；3-订单；4-合同；5-会员卡
     * @Author: luojie
     * @Date: 2021/6/8 15:34
     */
    @Override
    public Boolean insertExportService(int operateQuantity, String operateResult, String operateWay, String operateType) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        ImportExport importExport = new ImportExport();
        importExport.setImportExportId(CommonUtils.getGUID());
        importExport.setOperatePeopleId(userAccount.getAccountId());
        importExport.setOperateTime(new Date());
        importExport.setOperateQuantity(operateQuantity);
        importExport.setOperateResult(operateResult);
        importExport.setOperateWay(operateWay);
        importExport.setOperateType(operateType);
        Boolean result = importExportDbService.insert(importExport);
        return result;
    }

    @Override
    public int calculateImportedThreads(int listSize) {
        int threadNum = 0; // 记录线程数量
        //开启的线程数
        if (listSize > 0 && listSize < 100) {
            threadNum = 1;
        } else {
            if (listSize >= 1000 && listSize <= 3000) {
                threadNum = 20;
            } else{
                threadNum = 50;
            }/*else{
                    if ((listSize % 100) == 0) {
                        threadNum = listSize / 100;
                    } else {
                         threadNum = listSize / 100 + 1;
                    }
            }*/
        }

        return threadNum;
    }
}
